package com.tjtt.tdm.fileupload;

import java.io.IOException;
import java.io.PrintWriter;
import java.io.File;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import com.tjtt.tdm.exception.DaoException;
import com.tjtt.tdm.fileupload.bean.TdmAttachment;
import com.tjtt.tdm.fileupload.service.TdmAttachmentService;


public class DeleteFileServlet extends HttpServlet {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	/**
	 * Constructor of the object.
	 */
	public DeleteFileServlet() {
		super();
	}

	/**
	 * Destruction of the servlet. <br>
	 */
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

	/**
	 * The doGet method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to get.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		
	}

	/**
	 * The doPost method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to post.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		// 获取下载路径
		String id = request.getParameter("id");
		WebApplicationContext factory = WebApplicationContextUtils.getRequiredWebApplicationContext(request.getSession().getServletContext());  
		TdmAttachmentService tdmAttachmentService =  (TdmAttachmentService)factory.getBean("tdmAttachmentService");
		TdmAttachment attachment = null;
		try {
			attachment = tdmAttachmentService.getOneAttachment(id);
		} catch (DaoException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		String ctxDir = request.getSession().getServletContext().getInitParameter("filePath");
		if (!ctxDir.endsWith(String.valueOf(File.separatorChar))) {
			ctxDir = ctxDir + File.separatorChar;
		}
		String path = ctxDir + attachment.getFilePath();
		//System.out.println("filePath:"+filePath);
		//System.out.println("prjPath:"+prjPath);
		LogPrinter.info("删除文件："+path);
		PrintWriter out = response.getWriter();
		File file = new File(path);
		if(file.exists()){
			boolean flg = file.delete();
			if(flg){
				out.print("success");
				LogPrinter.info("删除文件成功："+path);
			}else{
				out.print("fail");
				LogPrinter.info("删除文件失败："+path);
			}
			
		}
	}

	/**
	 * Initialization of the servlet. <br>
	 *
	 * @throws ServletException if an error occurs
	 */
	public void init() throws ServletException {
		// Put your code here
	}

}
